package com.example.biji;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class NoteAdapter extends BaseAdapter implements Filterable {
    /*过滤器接口，用于列表中，通过添加过滤条件，刷新符合条件的数据并显示。*/
    private Context mContext;

    private List<Note> noteList;//这个数据是会改变的，所以要有个变量来备份一下原始数据
    private MyFilter mFilter;/*内部类*/

    public NoteAdapter(Context mContext, List<Note> noteList) {
        this.mContext = mContext;
        this.noteList = noteList;
    }

    @Override
    public int getCount() { return noteList.size(); }

    @Override
    public Object getItem(int position) { return noteList.get(position); }

    @Override
    public long getItemId(int position) { return position; }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PreferenceManager.getDefaultSharedPreferences(mContext);
        mContext.setTheme(R.style.DayTheme);/*给内容设置DayTheme风格*/
        View v = View.inflate(mContext, R.layout.note_layout, null);/*渲染note_layout布局*/
        TextView tv_content = v.findViewById(R.id.tv_content);/*应用tv_content样式*/
        TextView tv_time = v.findViewById(R.id.tv_time);/*应用tv_time样式*/
        ImageView list_img = v.findViewById(R.id.list_img);
        TextView tv_location = v.findViewById(R.id.tv_location);

        //Set text for TextView
        String allText = noteList.get(position).getContent();/*得到noteList，position位置上的内容*/
        String path = noteList.get(position).getPath();
        int flag = noteList.get(position).getFlag();

        tv_content.setText(allText);/*设置tv_content内容*/
        tv_time.setText(noteList.get(position).getTime());/*设置tv_time内容*/
        tv_location.setText(noteList.get(position).getLocation());
        if (flag == 0) {
            list_img.setImageBitmap((getImageThumbnail(path,200, 200)));
        }

        return v;
    }

    public static Bitmap getImageThumbnail(String uri, int width, int height) {
        Bitmap bitmap;
        BitmapFactory.Options options = new BitmapFactory.Options();
        int beWidth = options.outWidth / width;
        int beHeight = options.outHeight / height;
        int be;
        be = Math.min(beHeight, beWidth);
        if(be <= 0) {
            be = 1;
        }
        options.inSampleSize = be;
        bitmap = BitmapFactory.decodeFile(uri, options);
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

        return bitmap;
    }


    @Override
    public Filter getFilter() {
        if (mFilter == null){
            mFilter = new MyFilter();
        }
        return mFilter;
    }

    class MyFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            return null;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

        }
    }

}
